package com.zjp.birthdaymanager;

import android.app.Dialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zjp.birthdaymanager.database.DataBaseHelper;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class AddBirthActivity extends FragmentActivity implements View.OnClickListener {
    private static final String TAG = "AddBirthActivity";
    private TextView tv_title;

    private Button mSave;
    private Button mCancel;
    private EditText etName;
    private EditText etBirthdate;
    private EditText etPhone;
    private EditText etRemark;

    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_add_birth);

        DataBaseHelper DBHelper = new DataBaseHelper(AddBirthActivity.this, this.getString(R.string.dbname));

        db = DBHelper.getWritableDatabase();

        initView();

    }

    public void initView() {
        etName = (EditText) findViewById(R.id.et_name);
        etBirthdate = (EditText) findViewById(R.id.et_birthdate);
        etPhone = (EditText) findViewById(R.id.et_phone);
        etRemark = (EditText) findViewById(R.id.et_remark);
        tv_title = (TextView) findViewById(R.id.tv_titleBar_title);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("添加生日");

        mSave = (Button) findViewById(R.id.btn_save);
        mCancel = (Button) findViewById(R.id.btn_cancel);

        mSave.setOnClickListener(this);
        mCancel.setOnClickListener(this);
        etBirthdate.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_save:
                String strName = etName.getText().toString().trim();
                String strBirthDate = etBirthdate.getText().toString().trim();
                String strPhone = etPhone.getText().toString().trim();
                String strRemark = etRemark.getText().toString().trim();

                if (TextUtils.isEmpty(strName) || TextUtils.isEmpty(strBirthDate)) {

                    Toast.makeText(this, "名字不能为空", Toast.LENGTH_SHORT);
                } else {
                    ContentValues values = new ContentValues();
                    values.put("name", strName);
                    values.put("birthdate", strBirthDate);
                    values.put("phone", strPhone);
                    values.put("remark", strRemark);

                    long pid = db.insert("birth", null, values);

                    if (pid == -1) {
                        Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
                    } else {
                        finish();
                    }
                }

                break;
            case R.id.btn_cancel:
                finish();
                break;
            case R.id.et_birthdate:
                selectCheckDate();
                break;
        }

    }


    private CheckDateDialogFragment dialogFragment;

    public void selectCheckDate() {
        if (dialogFragment == null) {
            dialogFragment = new CheckDateDialogFragment();
        }
        dialogFragment.show(getSupportFragmentManager(), "checkInTime");
    }

    /**
     * 使用DialogFragment显示时间选择控件
     */
    public class CheckDateDialogFragment extends DialogFragment {
        AlertDialog dialog;
        String birthday = "2016-05-07";

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {

            View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_calendar_view, null);

            DatePicker datePicker
                    = (DatePicker) view.findViewById(R.id.datePicker);

            datePicker.setVisibility(View.VISIBLE);

            try {

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date date = sdf.parse(birthday);
                final Calendar c = Calendar.getInstance();
                c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
                c.setTime(date);


                datePicker.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
                    @Override
                    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        String month = (monthOfYear + 1) + "";
                        String day = dayOfMonth + "";
                        Log.i(TAG, "onDateChanged: " + year + (monthOfYear + 1) + dayOfMonth);
                        if ((monthOfYear + 1) < 10) {
                            month = "0" + month;
                        }
                        if (dayOfMonth < 10) {
                            day = "0" + day;
                        }
                        birthday = year + "-" + month + "-" + day;
                    }
                });


            } catch (Exception e) {
            }
            datePicker.setMaxDate(System.currentTimeMillis());

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

            builder.setView(view);
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    etBirthdate.setText(birthday);
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialog.dismiss();
                }
            });

            dialog = builder.create();
            return dialog;
        }

    }

}
